from pathlib import Path

import peewee

from emi.bar.data import Interval
from emi.util import SqliteUtil, NumUtils
from emi_gather.model import BarDataModel, storage
from emi_gather.model.impl._TickShotDB import TickShotDB
from emi_gather.model.storage import DefaultRealtimeDB, DBDriver

class SimpleDBDriver(DBDriver):
    def __init__(self):
        self.path = Path("tick_datas.db")
    def create_data_base(self,)->peewee.Database:
        return SqliteUtil.getDatabase(self.path, read_only=False)


# class SimpleDBDriver(DBDriver):
#     def __init__(self):
#         self.path = Path("/Users/raodongming/Desktop/gitee/emi-realtime-gather/gather-app/china_sub_stock2.db")
#     def create_data_base(self,)->peewee.Database:
#         return SqliteUtil.getDatabase(self.path, read_only=False)

db1 = DefaultRealtimeDB(db_driver=storage.ChinaStockDBDriver(),read_only=True)

symobl_list = db1.tick_db.get_code_list()
newest_day = db1.tick_db.get_newest_datetime()

tick_db = db1.tick_db
today_db = db1.tick_today_db
group_list = today_db.get_group_list(newest_day)

tick_target_db = TickShotDB(SimpleDBDriver().create_data_base())

tick_target_db.clear()
for index,group in enumerate(group_list):
    ticks = tick_db.get_tick_by_group(group =group,day=newest_day)
    print(f"p : {index}/{len(group_list)},ticks = {len(ticks)}")
    tick_target_db.save_bars(ticks)
    #assert len(db1.get_tick_data_list(code,newest_day)) == len(db2.get_tick_data_list(code,newest_day))




